//
//  HomeTopCell.swift
//  d11
//
//  Created by peale_pro on 2019/6/11.
//  Copyright © 2019年 Tech_001. All rights reserved.
//

import UIKit
typealias BtnClickBlock2 = (_ type: Int) -> Void
typealias BannerClickBlock2 = () -> Void
typealias MarqueeClickBlock2 = (_ index: Int) -> Void
class HomeTopCell: UITableViewCell,JJMarqueeViewDelegate,JJMarqueeViewDataSource,FSPagerViewDataSource,FSPagerViewDelegate  {
    var btnClickBlock2: BtnClickBlock2?
    func callChangeBlock(_ block: @escaping BtnClickBlock2) {
        
        btnClickBlock2 = block
    }
    var bannerClickBlock2: BannerClickBlock2?
    func callClickBlock(_ block: @escaping BannerClickBlock2) {
        
        bannerClickBlock2 = block
    }
    var marqueeClickBlock2: MarqueeClickBlock2?
    func callMarqueeBlock(_ block: @escaping MarqueeClickBlock2) {
        
        marqueeClickBlock2 = block
    }
    
    var topBgView:UIImageView?
    var marqueeBgView: UIView?
    var lImageView: UIImageView?
    var rImageView: UIImageView?
    var marqueeView : JJMarqueeView?
    var arrayList :[String]?
    var pagerView : FSPagerView?
    var imageUrls : [String]?
    var bannerBgImageView : UIImageView?
   // var pageControl : FSPageControl?
    
    var gridBtn1 : UIButton?
    var gridImgView1: UIImageView?
    var gridLabel1: UILabel?
    
    var gridBtn2 : UIButton?
    var gridImgView2: UIImageView?
    var gridLabel2: UILabel?
    
    var gridBtn3 : UIButton?
    var gridImgView3: UIImageView?
    var gridLabel3: UILabel?
    
    var gridBtn4 : UIButton?
    var gridImgView4: UIImageView?
    var gridLabel4: UILabel?
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.white
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.imageUrls = []
        self.arrayList = []
        
        self.topBgView = UIImageView.init(image: imageNamed("icon_home_head_bg"))
        self.contentView.addSubview(self.topBgView!)
        self.topBgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(184)
            make.top.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        self.marqueeBgView = UIView.init()
        self.marqueeBgView?.backgroundColor = UIColor.white
        self.marqueeBgView?.layer.masksToBounds = true
        self.marqueeBgView?.layer.cornerRadius = 28.5/2
        self.contentView.addSubview(self.marqueeBgView!)
        self.marqueeBgView?.snp.makeConstraints{(make) in
            make.height.equalTo(28.5)
            if isIphoneX_Series{
                  make.top.equalToSuperview().offset(100)
            }else{
                 make.top.equalToSuperview().offset(75)
            }
           
            make.right.equalToSuperview().offset(-44)
            make.left.equalToSuperview().offset(44)
        }
        self.lImageView = UIImageView.init(image: imageNamed("icon_home_news_l_r"))
        self.marqueeBgView?.addSubview(self.lImageView!)
        self.lImageView?.snp.makeConstraints{(make) in
            make.height.equalTo(5.5)
            make.width.equalTo(5)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(20)
        }
        
        self.rImageView = UIImageView.init(image: imageNamed("icon_home_news_l_r"))
        self.marqueeBgView?.addSubview(self.rImageView!)
        self.rImageView?.snp.makeConstraints{(make) in
            make.height.equalTo(5.5)
            make.width.equalTo(5)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-20)
        }
        
        self.marqueeView = JJMarqueeView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth-88-40-10-6, height:30.0))
        self.marqueeView?.automaticSlidingInterval = 3
        self.marqueeView?.delegate = self
        self.marqueeView?.dataSource = self
        self.marqueeView?.marqueeOneLab.textAlignment = .center
        self.marqueeView?.marqueeTwoLab.textAlignment = .center
        self.marqueeView?.marqueeOneLab.font = UIFont.systemFont(ofSize: 12)
        self.marqueeView?.marqueeTwoLab.font = UIFont.systemFont(ofSize: 12)
        self.marqueeBgView?.addSubview( self.marqueeView!)
        self.marqueeView?.snp.makeConstraints{(make) in
            make.left.equalTo((self.lImageView?.snp.right)!).offset(2)
            make.right.equalTo((self.rImageView?.snp.left)!).offset(-4)
            make.height.equalTo(30)
            make.centerY.equalToSuperview()
        }
        
        self.bannerBgImageView = UIImageView.init(frame: self.bounds)
        self.bannerBgImageView?.contentMode = .scaleAspectFit
        self.bannerBgImageView?.clipsToBounds = true
        self.bannerBgImageView?.image = imageNamed("img_default_banner")
        self.bannerBgImageView?.backgroundColor  = UIColor.clear
        self.contentView.addSubview(self.bannerBgImageView!)
        self.bannerBgImageView?.snp.makeConstraints{(make) in
            if kScreenWidth <= 320 {
                 make.height.equalTo(130)
                make.width.equalTo(self.bounds.width*0.8)
            }else{
                make.width.equalTo(self.bounds.width*0.97)
                make.height.equalTo(160)
            }
            
           
            make.top.equalTo((self.marqueeBgView?.snp.bottom)!).offset(18)
            make.centerX.equalTo(self.contentView)
        }
        
        self.pagerView = FSPagerView.init(frame: self.bounds)
        self.pagerView?.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        if kScreenWidth <= 320 {
            self.pagerView?.itemSize = CGSize.init(width: self.bounds.width*0.8, height: 130)
        }else{
            self.pagerView?.itemSize = CGSize.init(width: self.bounds.width*0.97, height: 160)
        }
      //  self.pagerView?.itemSize = CGSize.zero
        self.pagerView?.interitemSpacing = 10 //间距
        self.pagerView?.automaticSlidingInterval = 5 //间隔时间
        self.pagerView?.isInfinite = true //循环滚动
        self.pagerView?.dataSource = self
        self.pagerView?.delegate = self
        self.pagerView?.transformer = FSPagerViewTransformer(type:.linear)
     //    let transform = CGAffineTransform(scaleX: 0.99, y: 0.99)
     //    self.pagerView?.itemSize = (self.pagerView?.frame.size.applying(transform))!
        self.contentView.addSubview(pagerView!)
        self.pagerView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            if kScreenWidth <= 320 {
                 make.height.equalTo(130)
            }else{
                 make.height.equalTo(160)
            }
           
            make.centerX.equalTo(self.contentView)
            make.top.equalTo((self.marqueeBgView?.snp.bottom)!).offset(18)
        }
        
        self.gridBtn1 = UIButton.init(type: .custom)
        self.gridBtn1?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn1?.tag = 1001
        self.contentView.addSubview(self.gridBtn1!)
        self.gridBtn1?.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.left.equalToSuperview()
            make.height.equalTo(65)
            make.top.equalTo((self.pagerView?.snp.bottom)!).offset(15)
        }
        
        self.gridImgView1 = UIImageView.init(image: imageNamed("icon_home_ck2"))
        self.gridBtn1?.addSubview(self.gridImgView1!)
        self.gridImgView1?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel1 = UILabel.init()
        self.gridLabel1?.font = UIFont.systemFont(ofSize: 14)
        gridLabel1?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel1?.textAlignment = .center
        gridLabel1?.text = "存款"
        self.gridBtn1?.addSubview(self.gridLabel1!)
        self.gridLabel1?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView1?.snp.bottom)!).offset(5)
        }
        
        self.gridBtn2 = UIButton.init(type: .custom)
        self.gridBtn2?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn2?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn2?.tag = 1002
        self.contentView.addSubview(self.gridBtn2!)
        self.gridBtn2?.snp.makeConstraints{(make) in
           make.width.equalTo(kScreenWidth/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn1?.snp.right)!)
            make.top.equalTo((self.pagerView?.snp.bottom)!).offset(15)
        }
        
        self.gridImgView2 = UIImageView.init(image: imageNamed("icon_home_qk2"))
        self.gridBtn2?.addSubview(self.gridImgView2!)
        self.gridImgView2?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel2 = UILabel.init()
        self.gridLabel2?.font = UIFont.systemFont(ofSize: 14)
        gridLabel2?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel2?.textAlignment = .center
        gridLabel2?.text = "取款"
        self.gridBtn2?.addSubview(self.gridLabel2!)
        self.gridLabel2?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView2?.snp.bottom)!).offset(5)
        }
        
        self.gridBtn3 = UIButton.init(type: .custom)
        self.gridBtn3?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn3?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn3?.tag = 1003
        self.contentView.addSubview(self.gridBtn3!)
        self.gridBtn3?.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn2?.snp.right)!)
            make.top.equalTo((self.pagerView?.snp.bottom)!).offset(15)
        }
        self.gridImgView3 = UIImageView.init(image: imageNamed("icon_home_yh2"))
        self.gridBtn3?.addSubview(self.gridImgView3!)
        self.gridImgView3?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel3 = UILabel.init()
        self.gridLabel3?.font = UIFont.systemFont(ofSize: 14)
        gridLabel3?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel3?.textAlignment = .center
        gridLabel3?.text = "优惠"
        self.gridBtn3?.addSubview(self.gridLabel3!)
        self.gridLabel3?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView3?.snp.bottom)!).offset(5)
        }
        
        self.gridBtn4 = UIButton.init(type: .custom)
        self.gridBtn4?.setTitleColor(UIColor.black, for: .normal)
        self.gridBtn4?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.gridBtn4?.tag = 1004
        self.contentView.addSubview(self.gridBtn4!)
        self.gridBtn4?.snp.makeConstraints{(make) in
            make.width.equalTo(kScreenWidth/4)
            make.height.equalTo(65)
            make.left.equalTo((self.gridBtn3?.snp.right)!)
            make.top.equalTo((self.pagerView?.snp.bottom)!).offset(15)
        }
        self.gridImgView4 = UIImageView.init(image: imageNamed("icon_home_zd2"))
        self.gridBtn4?.addSubview(self.gridImgView4!)
        self.gridImgView4?.snp.makeConstraints{(make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.top.equalTo(2)
        }
        
        
        self.gridLabel4 = UILabel.init()
        self.gridLabel4?.font = UIFont.systemFont(ofSize: 14)
        gridLabel4?.textColor = HexRGBAlpha(0x747474, 1)
        gridLabel4?.textAlignment = .center
        gridLabel4?.text = "投注记录"
        self.gridBtn4?.addSubview(self.gridLabel4!)
        self.gridLabel4?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo((self.gridImgView4?.snp.bottom)!).offset(5)
        }
    }

    func setData(imgUrls:[String]? ,arrayList:[String]?) -> Void {
        self.imageUrls = imgUrls
        self.arrayList = arrayList
        self.marqueeView?.reload()
//        self.pageControl?.numberOfPages = self.imageUrls?.count ?? 0
//        self.pageControl?.currentPage = self.pagerView?.currentIndex ?? 0
        self.pagerView?.reloadData()
        if self.imageUrls?.count ?? 0 == 0{
            self.bannerBgImageView?.isHidden = false
        }else{
            self.bannerBgImageView?.isHidden = true
        }
    }
    
    func numberOfItems(_ marqueeView: JJMarqueeView) -> Int {
        return arrayList?.count ?? 0
    }
    func marqueeView(_ marqueeView: JJMarqueeView, cellForItemAt index: Int) -> NSAttributedString {
        print(index)
        let att = NSMutableAttributedString.init(string: arrayList?[index] ?? "")
        return att
        
    }
    func mqrqueeView(_ marqueeView:JJMarqueeView,didSelectCellAt index:Int) -> Void
    {
        print("!!!!!!!\(index)")
        if marqueeClickBlock2 != nil {
            marqueeClickBlock2!(index)
        }
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        //img_default_banner
        cell.imageView?.sd_setImage(with: URL.init(string: self.imageUrls![index]), placeholderImage: imageNamed("img_default_banner"), options: .refreshCached, completed: nil);
        //        cell.imageView?.sd_setImage(with: URL.init(string: self.imageUrls![index]), completed: { (img, error, type, url) in
        //
        //        })
        cell.imageView?.contentMode = .scaleToFill
        cell.imageView?.clipsToBounds = true
        //      cell.textLabel?.text = index.description+index.description
        return cell
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.imageUrls?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
//        self.pageControl?.currentPage = index
        if bannerClickBlock2 != nil{
            bannerClickBlock2!()
        }
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
      //  guard self.pageControl?.currentPage != pagerView.currentIndex else {
//            return
//        }
       // self.pageControl?.currentPage = pagerView.currentIndex // Or Use KVO with property "currentIndex"
    }
    
    @objc func actionToClick(_ sender:UIButton) -> Void {
        if btnClickBlock2 != nil{
            btnClickBlock2!(sender.tag)
        }
    }
}
